package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.service.IUserService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private EmployeeMapper employeeMapper;
    // 登陆验证
    @Override
    public JsonResult login(String username, String password)  {
        // 去数据库查询用户
        Employee employee = employeeMapper.selectByUser(username, password);
        // 如果用户存在返回登陆成功
        if (employee != null ){
            // 把登陆的用户放进Session
            UserContext.setCurrentUser(employee);
            return new JsonResult(true,"登陆成功");
        }
        // 用户不存在返回自定义异常
        throw new RuntimeException("用户名或密码错误");
    }

    @Override
    public void state(Long id) {
        //查出要改状态的用户信息
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        //把他状态取反设置回去
        employee.setState(!employee.isState());
        //执行修改sql
        employeeMapper.updateByPrimaryKey(employee);
    }
}
